package com.cnt.controller;

import com.cnt.entity.User;
import com.cnt.entity.UserInfo;
import com.cnt.service.UserInfoService;
import com.cnt.service.UserService;
import com.cnt.util.TokenUtil;
import com.cnt.vo.ResultVo;
import com.cnt.vo.UserResultVo.LoginResultVo;
import com.cnt.vo.UserResultVo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/getUserInfo")
    public ResultVo<UserInfo> getUserInfo(@RequestParam Integer userId) {
        return userInfoService.getUserInfo(userId);
    }

    @PutMapping("/updateUserInfo")
    public ResultVo<UserInfo> updateUserInfo(@RequestBody UserInfo userInfo) {
        return userInfoService.updateUserInfo(userInfo);
    }

    @PostMapping("/register")
    public ResultVo<String> register(@RequestBody User user) {

        return userService.userRegister(user);
    }

    @PostMapping("/checkAccount")
    public ResultVo<String> checkAccount(@RequestBody User user) {

        return userService.checkAccount(user.getAccount());
    }

    @PostMapping("/checkIdCard")
    public ResultVo<String> checkIdCard(@RequestBody User user) {

        return userService.checkIdCard(user.getIdCard());
    }


    @PostMapping("/login")
    public ResultVo<LoginResultVo> login(@RequestBody Map<String, String> requestBody, HttpServletResponse response) {
        String account = requestBody.get("account");

        String password = requestBody.get("password");
        System.out.println(account + password);
        // 验证用户名和密码
        ResultVo<LoginResultVo> resultVo = userService.login(account, password);

//        if (resultVo.getCode() == 0) {
//            int userId = resultVo.getData().getUserId();
//            System.out.println("login===" + userId);
//            // 生成Token并返回给前端
//            String token = TokenUtil.generateToken("" + userId);
//            System.out.println("1" + token);
//            // 将 Token 存储到相应的用户中
//            TokenUtil.saveToken(userId, token);
//            response.setHeader("Authorization", "Bearer " + token);
//        }

        return resultVo;
    }

    // 处理注销请求
    @PostMapping("/logout")
    public ResultVo<String> logout(HttpServletRequest request) {
        // 获取 Token，并从相应的用户中删除
        String token = TokenUtil.getTokenFromRequest(request);
        System.out.println("logout==>" + token);
        TokenUtil.removeToken(token);
        return new ResultVo<>(0, "成功退出", null);
    }


    @PostMapping("/changePassword")
    public ResultVo<String> changePassword(@RequestBody Map<String, String> requestBody) {
        String account = requestBody.get("account");
        String idCard = requestBody.get("idCard");
        String newPassword = requestBody.get("newPassword");
        System.out.println(account + newPassword + idCard);
        ResultVo<String> result = userService.changePassword(account, idCard, newPassword);
        return result;
    }

    @PostMapping("/updateImage")
    public ResultVo<String> updateImage(@RequestParam("userId") Integer userId,
                                        @RequestParam("file") MultipartFile file) {
        return userInfoService.updateImage(userId, file);
    }

    @GetMapping("/showHead")
    public ResultVo<String> showHead(@RequestParam Integer userId) {
        return userInfoService.showHead(userId);
    }

    @GetMapping("/getUserInfoVo")
    public ResultVo<UserInfoVo> getUserInfoVo(@RequestParam Integer userId) {
        return userInfoService.getUserInfoVo(userId);
    }

}
